﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace YTSK.Frm
{

    public class FrmSkTotalCount :Vouch.VouchData
    {
        public FrmSkTotalCount(string cvouchtype, Control c) :
            base(cvouchtype, c)
        {
            base._dg.CellValidating += dataGridView1_CellValidating;
        }

        #region 数据验证
        private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            var dgc = _dg.Columns[e.ColumnIndex];
            var colname = dgc.Name;
            var list = new List<string>() { "iMoneySale", "iTaxSale", "iMoneyLW" };
            if (!list.Contains(colname)) { return; }

            //数量改变：金额=数量*单价；不含税金额；税额：折前金额=金额+折扣
            //单价改变：金额=数量*单价；不含税金额；税额：折前金额=金额+折扣
            //金额改变: 单价=金额/数量；不含税金额；税额：折前金额=金额+折扣
            //iQuotePrice折前金额改变：折扣=折前金额-金额
            //iDiscountPrice折扣改变：折前金额=金额+折扣

            var dgr = _dg.Rows[e.RowIndex];
            var itaxrate = dgr.Cells["iTaxRateSale"].Value.ObjToString().Replace("%", "").ObjToDecimal() / 100;
            var imoneysale = dgr.Cells["iMoneySale"].Value.ObjToDecimal();
            var itaxsale = dgr.Cells["iTaxSale"].Value.ObjToDecimal();
            var imoneylw = dgr.Cells["iMoneyLW"].Value.ObjToDecimal();
            var itaxlw = dgr.Cells["iTaxLW"].Value.ObjToDecimal();

            switch (colname)
            {
                case "iMoneySale":
                    imoneysale = e.FormattedValue.ObjToDecimal();
                    itaxsale = imoneysale *itaxrate;
                    break;
                case "iMoneyLW":
                    imoneylw = e.FormattedValue.ObjToDecimal();
                    itaxlw = imoneylw * itaxrate;
                    break;
            }
            dgr.Cells["iTaxSale"].Value = Math.Round(itaxsale, 2);
            dgr.Cells["iTaxLW"].Value = Math.Round(itaxlw, 2);
            base.sumcolAll();
        }
        #endregion


        public Dictionary<string, object> GetVouchData()
        {
            var dicmain = new Dictionary<string, object>();
            var list = new List<Dictionary<string, object>>();
            var vmain = base._vouchs.Where(x => x.iHead == 1).ToList();
            
            var vdetail = base._vouchs.Where(x => x.iHead == 2 && x.FieldCode != "dataGrid").ToList();
            for (int i = 0; i < base._dg.Rows.Count; i++)
            {
                var dgr = base._dg.Rows[i];
                var dic = new Dictionary<string, object>();
                for (int j = 0; j < base._dg.Columns.Count; j++)
                {
                    var col = base._dg.Columns[j];
                    dic.Add(col.Name, dgr.Cells[j].Value);
                }
                list.Add(dic);
            }
            
            var d = new Dictionary<string, object>();
            d.Add("SK_TotalModel", list);
            return d;
        }
    }
}
